package com.xgkj.diyiketang.security;

public class StringUtil {
	public static byte[] hexToByte(String hex) {
		   int len = (hex.length() / 2);
		   byte[] result = new byte[len];
		   char[] achar = hex.toCharArray();
		   for (int i = 0; i < len; i++) {
			   int pos = i * 2;
			   result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
		   }
		   return result;
		}
	
	private static byte toByte(char c) {
		   byte b = (byte) "0123456789ABCDEF".indexOf(c);
		   return b;
		}
	
	public static String byteToHex(byte[] b) {
		try{
			StringBuffer sb = new StringBuffer(b.length * 2);
			for (int i = 0; i < b.length; i++) {
				byte v = (byte) (b[i] & 0xff);
				if (v < 16) {
					sb.append('0');
				}
				sb.append(Integer.toHexString(v));
			}
			return sb.toString().toUpperCase();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

}
